﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.foreman
{
    public partial class foreman_apply_edit : Web.UI.ManagePage
    {
        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.foreman_apply().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("foreman_apply_edit", DTEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(); //绑定所有工长
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }
        #region 绑定类别=================================
        private void TreeBind()
        {
            BLL.foreman_user bll = new BLL.foreman_user();
            DataTable dt = bll.GetList(0,"status=0","nick_name asc").Tables[0];

            this.ddlForemanId.Items.Clear();
            this.ddlForemanId.DataSource = dt;
            this.ddlForemanId.DataTextField = "nick_name";
            this.ddlForemanId.DataValueField = "id";
            this.ddlForemanId.DataBind();
            this.ddlForemanId.Items.Insert(0,new ListItem("请选择工长...", "0"));
        }
        #endregion
        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.foreman_apply bll = new BLL.foreman_apply();
            var model = bll.GetModel(_id);

            rblType.SelectedValue = model.apply_type.ToString();
            ddlForemanId.SelectedValue = model.foreman_id.ToString();
            txtName.Text = model.apply_name;
            txtPhone.Text = model.apply_phone;
            txtCity.Text = model.apply_city;
            txtSummary.Text = model.apply_summary;
            txtInfo.Text = model.apply_info;
            rblSrc.SelectedValue = model.apply_src.ToString();
            rblStatus.SelectedValue = model.apply_status.ToString();
            txtNote.Text = model.apply_note;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.foreman_apply model = new Model.foreman_apply();
            BLL.foreman_apply bll = new BLL.foreman_apply();

            model.apply_type = int.Parse(rblType.SelectedValue);
            model.foreman_id = int.Parse(ddlForemanId.SelectedValue);
            model.apply_name = Utils.DropHTML(txtName.Text);
            model.apply_phone = Utils.DropHTML(txtPhone.Text);
            model.apply_city = Utils.DropHTML(txtCity.Text);
            model.apply_summary = Utils.DropHTML(txtSummary.Text);
            model.apply_info = Utils.DropHTML(txtInfo.Text);
            model.apply_src = int.Parse(rblSrc.SelectedValue);
            model.apply_status = int.Parse(rblStatus.SelectedValue);
            model.apply_note = Utils.DropHTML(txtNote.Text);
            model.apply_addtime = DateTime.Now;

            
            if (bll.Add(model)>0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加预约:" + model.apply_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.foreman_apply bll = new BLL.foreman_apply();
            Model.foreman_apply model = bll.GetModel(_id);

            model.apply_type = int.Parse(rblType.SelectedValue);
            model.foreman_id = int.Parse(ddlForemanId.SelectedValue);
            model.apply_name = Utils.DropHTML(txtName.Text);
            model.apply_phone = Utils.DropHTML(txtPhone.Text);
            model.apply_city = Utils.DropHTML(txtCity.Text);
            model.apply_summary = Utils.DropHTML(txtSummary.Text);
            model.apply_info = Utils.DropHTML(txtInfo.Text);
            model.apply_src = int.Parse(rblSrc.SelectedValue);
            model.apply_note = Utils.DropHTML(txtNote.Text);
            if (model.apply_status == 1 && rblStatus.SelectedValue == "2")
            {
                model.apply_handler = GetAdminInfo().id;
                model.apply_handtime = DateTime.Now;
            }

            model.apply_status = int.Parse(rblStatus.SelectedValue);

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "修改预约信息:" + model.apply_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("foreman_apply_edit", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改预约成功！", "foreman_apply_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("foreman_apply_edit", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加预约成功！", "foreman_apply_list.aspx");
            }
        }

    }
}